﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
namespace GeradorMassaDados.Bovespa.Business
{
    [DataContract]
    public class CollectItem
    {
        #region Field

        static string fileStoragePath;

        #endregion

        #region Properties

        /// <summary>
        /// Caminho que o arquivo com os dados dessa classe é persistido
        /// </summary>
        [IgnoreDataMember]
        private static string FileStoragePath
        {
            get 
            {
                if (null == fileStoragePath)
                    InitializeFileStorage();
                return fileStoragePath;
            }
        }

        /// <summary>
        /// Data e horário de ínicio da coleta
        /// </summary>
        [DataMember]
        public DateTime StartTime { get; private set; }

        /// <summary>
        /// Nome do usuário de rede associado a massa de dados
        /// </summary>
        [DataMember]
        public string Terminal { get; private set; }

        // <summary>
        /// Data e horário do fim da coleta
        /// </summary>
        [DataMember]
        public DateTime EndTime { get; private set; }
        
        #endregion

        #region Constructor
        
        /// <summary>
        /// Construtor padrão
        /// </summary>
        /// <param name="start">Data e horário de ínicio da coleta</param>
        /// <param name="userName">Nome do usuário de rede associado a massa de dados</param>
        public CollectItem(DateTime start, string userName)
        {
            this.StartTime = start;
            this.Terminal = userName;
        }

        #endregion

        #region Methods

        static void InitializeFileStorage()
        {
            fileStoragePath = Path.Combine(
                Path.GetTempPath(),
                AppDomain.CurrentDomain.FriendlyName);

            if (!Directory.Exists((fileStoragePath)))
                Directory.CreateDirectory(fileStoragePath);

            fileStoragePath = Path.Combine(fileStoragePath, "CollectItem.xml");
        }
               
        public void SetEndTime()
        {
            EndTime = new DataAcess().GetOracleDate();
        }

        public void Save()
        {
            this.Delete();

            var serializer = new NetDataContractSerializer();
            using(var fileStream = new FileStream(FileStoragePath, FileMode.CreateNew, FileAccess.Write, FileShare.None))
            {
                serializer.Serialize(fileStream, this);
                fileStream.Flush();
                fileStream.Close();
            }
        }

        public void Delete()
        {
            if (File.Exists(FileStoragePath)) File.Delete(FileStoragePath);
        }

        public static CollectItem Load()
        {
            if (!File.Exists(FileStoragePath))
                return null;
                        
            var serializer = new NetDataContractSerializer();
            using (var fileStream = new FileStream(FileStoragePath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                return (CollectItem)serializer.Deserialize(fileStream);
            }
        }
        
        /// <summary>
        /// Método para teste auxiliar nos testes
        /// </summary>
        internal void Test()
        {
#if DEBUG
            StartTime = DateTime.Now.AddDays(-1); //new DateTime(2010, 09, 17, 10, 35, 34);
            EndTime = DateTime.Now.AddDays(1); //new DateTime(2010, 09, 17, 10, 35, 36);
#endif
        }

        #endregion

    }
}
